﻿using System;
using System.Web;
using Castle.MicroKernel;
using Castle.MicroKernel.Context;
using Castle.MicroKernel.Lifestyle;

namespace RuTracker.WebSite.IoC
{
    public class PerSessionLifestyleManager : AbstractLifestyleManager
    {
        private readonly string PerSessionObjectID = "PerSessionLifestyleManager_" + Guid.NewGuid().ToString();

        public override object Resolve(CreationContext context, IReleasePolicy policy)
        {
            if (HttpContext.Current == null)
                return base.Resolve(context, policy);

            if (HttpContext.Current.Session[PerSessionObjectID] == null)
            {
                // Create the actual object
                HttpContext.Current.Session[PerSessionObjectID] = base.Resolve(context, policy);
            }

            return HttpContext.Current.Session[PerSessionObjectID];
        }

        public override void Dispose()
        {
        }
    }
}
